//
//  MessageViewController.m
//  EnterpriseEdition
//
//  Created by ypj on 2021/10/19.
//

#import "MessageViewController.h"
#import "RCConversationListViewController.h"
#import "LXSystemMessageCell.h"
#import "LXConverstaionViewController.h"
#import <RCIM.h>
#import <RongIMKit.h>
#import "RCPluginBoardItem.h"
#import <RCVoiceRoomLib/RCVoiceRoomLib.h>
#import <RongIMLib/RongIMLib.h>
#import "LXSysMessageViewController.h"
#import "LXSystemMsgModel.h"
@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic)LXSystemMessageCell *view1;
@property(nonatomic)LXSystemMessageCell *view2;

@end

@implementation MessageViewController

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    if (self.navigationController.viewControllers.count > 1) {
        self.tabBarController.tabBar.hidden = YES;
    } else {
        self.tabBarController.tabBar.hidden = NO;
    }
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    
    [self loadData];
}
- (void)viewDidLoad {
     [super viewDidLoad];
     self.view.backgroundColor=White_Color;
    
//    self.title = @"消息";
//    [self layoutCustomNav:self.view title:@"消息"];
  
    
    UILabel *titleLB = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth / 2 - 100, KStatuesBarHeight, 200, 40)];
    titleLB.text = @"消息";
    titleLB.textColor = ColorWith333333;
    titleLB.textAlignment = NSTextAlignmentCenter;
    titleLB.font = [UIFont boldSystemFontOfSize:17];
    [self.view addSubview:titleLB];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, kViewTopHeight-1, ScreenWidth, 1)];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#E7EDF2"];
    [self.view addSubview:lineView];
    
    
    
    
    RCKitConfigCenter.ui.globalMessageAvatarStyle = RC_USER_AVATAR_CYCLE;

    [self layoutSystemMessage];
    
    self.conversationListTableView.frame = CGRectMake(0, self.view2.Y_height, ScreenWidth, kScreenHeight - self.view2.Y_height - LL_TabbarHeight);
    

 }
-(void)loadData{
    // 系统消息未读消息
    [MSNetworkManager getRevertUrl:[DOMAIN_URL stringByAppendingString:GET_GetMessageList] params:@{@"page":@(1),@"rows":@(10000),@"type":@(1),@"userCode":@"1"} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            LXSystemMsgModel *model = [[LXSystemMsgModel mj_objectArrayWithKeyValuesArray:resonseObj[@"list"][@"records"]] firstObject];
            if (model.title) {
                self.view1.contentLB.text = model.title;
            }
            else {
                self.view1.contentLB.text = @"您还没有新消息";
            }
            if(!StringIsEmpty(model.createTime)){
                self.view1.timeLB.text = [NSString timestampSwitchTime:[NSString timeSwitchTimestamp:model.createTime andFormatter:@"yyyy-MM-dd HH:mm:ss"] / 1000 andFormatter:@"HH:mm"];
                if ([resonseObj[@"unRead"] intValue] == 0) {
                    self.view1.badgeLB.hidden = YES;
                }
                else {
                    self.view1.badgeLB.text = [NSString stringWithFormat:@"%@",@([resonseObj[@"unRead"] intValue])];
                    self.view1.badgeLB.hidden = NO;
                }
            }
        }
      
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        
    } withShowHud:NO];

    // 订单未读消息
    [MSNetworkManager getRevertUrl:[DOMAIN_URL stringByAppendingString:GET_GetMessageList] params:@{@"page":@(1),@"rows":@(10000),@"type":@(4),@"userCode":@"1"} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            {
               
                LXSystemMsgModel *model = [[LXSystemMsgModel mj_objectArrayWithKeyValuesArray:resonseObj[@"list"][@"records"]] firstObject];
                if (model.title) {
                    self.view2.contentLB.text = model.title;
                }
                else {
                    self.view2.contentLB.text = @"您还没有新消息";
                }
                if(!StringIsEmpty(model.createTime)){
                    self.view2.timeLB.text = [NSString timestampSwitchTime:[NSString timeSwitchTimestamp:model.createTime andFormatter:@"yyyy-MM-dd HH:mm:ss"] / 1000 andFormatter:@"HH:mm"];
                    if ([resonseObj[@"unRead"] intValue] ==0) {
                        self.view2.badgeLB.hidden = YES;
                    }
                    else {
                        self.view2.badgeLB.text = [NSString stringWithFormat:@"%@",@([resonseObj[@"unRead"] intValue])];
                        self.view2.badgeLB.hidden = NO;
                    }
                }
            }
        }
      
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        
    } withShowHud:NO];
}
- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType conversationModel:(RCConversationModel *)model atIndexPath:(NSIndexPath *)indexPath{
    
    LXConverstaionViewController *conversationVC = [[LXConverstaionViewController alloc] initWithConversationType:model.conversationType targetId:model.targetId];
    conversationVC.conversationType = model.conversationType;
    conversationVC.targetId = model.targetId;
    conversationVC.textStr = model.conversationTitle;
    if (model.conversationModelType == RC_CONVERSATION_MODEL_TYPE_NORMAL) {
        conversationVC.unReadMessage = model.unreadMessageCount;
        conversationVC.enableNewComingMessageIcon = YES; //开启消息提醒
        conversationVC.enableUnreadMessageIcon = YES;
    }
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:conversationVC animated:YES];
    
    
}
// 站内信
-(void)layoutSystemMessage{
    
    
    self.view1 = [[LXSystemMessageCell alloc] initWithFrame:CGRectMake(0, kViewTopHeight, ScreenWidth, 70)];
    [self.view1 whenTapped:^{
       
        LXSysMessageViewController *vc = [[LXSysMessageViewController alloc] init];
        vc.type = 1;
        self.tabBarController.tabBar.hidden = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
        
    }];
    [self.view addSubview:self.view1];
    self.view1.iconImg.image = [UIImage imageNamed:@"系统消息"];
    [self.view1.iconImg sizeToFit];
    self.view1.titleLB.text = @"系统消息";
    self.view1.contentLB.text = @"您还没有新消息";
    
    
    self.view2 = [[LXSystemMessageCell alloc] initWithFrame:CGRectMake(0, self.view1.Y_height, ScreenWidth, 70)];
    [self.view addSubview:self.view2];
    self.view2.iconImg.image = [UIImage imageNamed:@"补车提醒"];
    [self.view2.iconImg sizeToFit];
    self.view2.titleLB.text = @"补车提醒";
    self.view2.contentLB.text = @"您还没有新消息";
    [self.view2.lineView removeFromSuperview];
    [self.view2 whenTapped:^{
        LXSysMessageViewController *vc = [[LXSysMessageViewController alloc] init];
        vc.type = 4;
        self.tabBarController.tabBar.hidden = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
    }];

}
@end
